﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.International.Converters.PinYinConverter;

namespace Tools
{
   public static  class GetPy
    {
        /// <summary>
        /// 在指定的字符串列表CnStr中检索符合拼音索引字符串
        /// </summary>
        /// <param name="CnStr">汉字字符串</param>
        /// <returns>相对应的汉语拼音首字母串</returns>
        public static string GetPingYingFirstLetter(string input)
        {
            string s = string.Empty;
            foreach (char c in input)
            {
                s += GetFirstChar(c.ToString());
            }
            return s;
        }
        public static char GetFirstChar(string name)
        {
            var c = name.First();
            if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
            {
                return c;
            }
            else
            {
                try
                {
                    ChineseChar cc = new ChineseChar(c);
                    if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
                    {
                        return cc.Pinyins[0][0];
                    }
                }
                catch 
                {
                    return c;
                }
                return c;
            }
        }
    }
}
